<%-- 
    Document   : empleados
    Created on : 1/06/2011, 01:23:26 AM
    Author     : Fernando
--%>

<%@page import="controller.UsuariosRolesMgr"%>
<%@page import="model.UsuariosRoles"%>
<%@page import="controller.RolesMgr"%>
<%@page import="java.util.ArrayList"%>
<%@page import="controller.EmpresasMgr"%>
<%@page import="model.Empresas"%>
<%@page import="java.util.List"%>
<%@page import="model.Roles"%>
<%@page import="controller.UsuariosMgr"%>
<%@page import="model.Usuarios"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
            HttpSession currentSession = request.getSession();
            String doc = currentSession.getAttribute("id") == null ? "" : (String) currentSession.getAttribute("id");
            String html = "";

            if ( doc == null || doc.isEmpty() ) {
                response.sendRedirect("/GGP/index.jsp");

            } else {

             ArrayList<Roles> lstRolesActual = RolesMgr.mgr.getListPorUsuario(doc);
             Roles rolActual = new Roles();
             if (lstRolesActual.size() > 0) {
                    rolActual = lstRolesActual.get(0);
              }

            Usuarios user = UsuariosMgr.mgr.getUsuario(doc);
            Empresas miEmpresa=EmpresasMgr.mgr.getItemForNit(user.nitEmpresa);

            String opt = request.getParameter("opt") == null ? "" : request.getParameter("opt");


            String alert = "";

           

            String opc = request.getParameter("opc") == null ? "" : request.getParameter("opc");
            String editar = request.getParameter("editar") == null ? "" : request.getParameter("editar");



                    String idEmpleado = request.getParameter("idEmpleado") == null ? "" : request.getParameter("idEmpleado");
                    Usuarios data = UsuariosMgr.mgr.getItemForCedula(idEmpleado);

                    if (opc.equals("guardar")) {
                        data.nombre = request.getParameter("nombre") == null ? "" : request.getParameter("nombre");
                        data.cedula = request.getParameter("cedula") == null ? "" : request.getParameter("cedula");
                        //data.clave = request.getParameter("clave") == null ? "" : request.getParameter("clave");
                        if (!editar.equals("1"))
                            data.clave = "1234";
                        data.telefono = request.getParameter("telefono") == null ? "" : request.getParameter("telefono");
                        data.correo = request.getParameter("correo") == null ? "" : request.getParameter("correo");
                        data.nitEmpresa = user.nitEmpresa;
                        String rol = request.getParameter("permisos") == null ? "" : request.getParameter("permisos");
                        if (!editar.equals("1")) {
                            UsuariosMgr.mgr.save(data);
                        } else {
                            UsuariosMgr.mgr.actualizarUsuario(data);
                            if (!rol.isEmpty()) UsuariosRolesMgr.mgr.eliminarRolesUsuario(data.cedula);
                        }
                        if (!rol.isEmpty()) {
                            UsuariosRoles ur = new UsuariosRoles();
                            ur.cedulaUsuario = data.cedula;
                            ur.rolUsuario = rol;
                            UsuariosRolesMgr.mgr.save(ur);
                        }
                        response.sendRedirect("empleados.jsp");

                    }

                    if (opc.equals("guardar_passwd")){
                        String clave = request.getParameter("clave") == null ? "" : request.getParameter("clave");
                        String confClave = request.getParameter("confClave") == null ? "" : request.getParameter("confClave");

                        if (clave.equals(confClave)){
                            UsuariosMgr.mgr.cambiarClave(idEmpleado, clave);
                            alert = "La Contraseña del usuario ha sido reestablecida";
                        }else{
                            alert = "Las Contraseñas ingresadas no coinciden";
                        }
                        opc = "alert";

                    }

                    if (opc.equals("eliminar")) {

                        UsuariosMgr.mgr.eliminarUsuario(idEmpleado);
                    }

                    ArrayList<Usuarios> lst = UsuariosMgr.mgr.getListForEmpresa(user.nitEmpresa);

                    html = "<table class='sortable' cellpadding='2' cellspacing='0'><thead>"
                            + "<tr>"

                            + "<th>CÉDULA</td>"
                            + "<th>NOMBRE</td>"
                            + "<th>TELEFONO</td>"
                            + "<th>CORREO</td>"
                            + "<th>ROL</td>"

                            + "<th class='nosort'><img border='0' src='../images/edit.png' /></td>"
                            + "<th class='nosort'><img border='0' src='../images/define_permissions.png' /></td>"
                            + "<th class='nosort'><img border='0' src='../images/12.png' /></td></tr></thead><tbody>";

                    for (Usuarios it : lst) {
                        html += "<tr>" + it.getHtmlRow() + "<td>" + RolesMgr.mgr.getListPorUsuario(it.cedula).get(0).nombreRol + "</td>";

                        html += "<td align='center' class='st4right'><form title='' method='post' action='empleados.jsp' target='_self'>"
                                + "<input type='hidden' name='idEmpleado' value='" + it.cedula + "' />"
                                + "<input type='hidden' name='opc' value='editar' />"
                                + "<input type='hidden' name='opt' value='empleados' />"
                                + "<input type='image' border='0' title='editar' src='../images/edit.png' /></form></td>";

                        html += "<td align='center' class='st4right'><form title='' method='post' action='empleados.jsp' target='_self'>"
                                + "<input type='hidden' name='idEmpleado' value='" + it.cedula + "' />"
                                + "<input type='hidden' name='opc' value='passwd' />"
                                + "<input type='hidden' name='opt' value='empleados' />"
                                + "<input type='image' border='0' title='contraseña' src='../images/define_permissions.png' /></form></td>";

                        if (it.cedula.equals(doc)) {
                         html += "<td>&nbsp;</td></tr>";
                        } else {
                        html += "<td align='center' class='st4right'><form title='' method='post' action='empleados.jsp' target='_self'>"
                                + "<input type='hidden' name='idEmpleado' value='" + it.cedula + "' />"
                                + "<input type='hidden' name='opc' value='eliminar' />"
                                + "<input type='hidden' name='opt' value='empleados' />"
                                + "<input type='image' border='0' title='eliminar' src='../images/12.png' /></form></td></tr>";
                        }
                    }

                    html += "</tbody><tfoot><tr><td class='st3' align='right' colspan='8'><form title='Agregar Empleados' method='post' action='empleados.jsp' target='_self'>"
                            + "<input type='hidden' name='opc' value='agregar' />"
                            + "<input type='hidden' name='opt' value='empleados' />"
                            + "<input type='image' border = '0' title='agregar' src='../images/11.png' />"
                            + "</form></td></tr></tfoot></table>";

                    if (opc.equals("editar") || opc.equals("agregar")) {

                        editar = "";
                        if (!data.cedula.equals("")) {
                            editar = "1";
                        }


                        html += "<br><br><form method='post' name='formAdd' target='_self' action='empleados.jsp' onsubmit='return validarFormEmpleados()' >"
                                + "<table width='' cellpadding='3' cellspacing='0'><input type='hidden' name='opc' value='guardar' />"
                                + "<input type='hidden' name='opt' value='empleados' />"
                                + "<input type='hidden' name='editar' value='" + editar + "' />"
                                + "<tr><td colspan='2' class='st1'>Empleado</td></tr>";

                        if (editar.equals("1")) {
                            html += "<tr><td class='st3'>Cédula:</td><td class='st4'>" + data.cedula + "&nbsp;</td></tr>"
                                    + "<input type='hidden' id='cedula' name='cedula' value='" + data.cedula + "' />";
                        } else {
                            html += "<tr><td class='st3'>Cédula :</td><td class='st4'><input type='text' id='cedula' name='cedula' value ='" + data.cedula + "' size='50' /></td></tr>";
                        }

                        ArrayList<Roles> lstRoles = RolesMgr.mgr.getListPorUsuario(idEmpleado);
                        Roles rol = new Roles();
                        if (lstRoles.size() > 0) {
                            rol = lstRoles.get(0);
                        }

                        lstRoles = RolesMgr.mgr.getListForEmpresa(user.nitEmpresa);

                        html += "<tr><td class='st3'>Nombre :</td><td class='st4'><input type='text' id='nombre' name='nombre' value ='" + data.nombre + "' size='50' /></td></tr>"
                                //+ "<tr><td class='st3'>Clave:</td><td class='st4'><input type='password' id='clave' name='clave' value ='" + data.clave + "' size='50' /></td></tr>"
                                + "<tr><td class='st3'>Teléfono:</td><td class='st4'><input type='text' id='telefono' name='telefono' value ='" + data.telefono + "' size='50' /></td></tr>"
                                + "<tr><td class='st3'>Correo:</td><td class='st4'><input type='text' id='correo' name='correo' value ='" + data.correo + "' size='50' /></td></tr>"
                                + "<tr><td class='st3'>Rol:</td><td class='st4'>";

                        if (!rol.permisos.equals("1")) {
                            html += "<select name='permisos' id='permisos'>";

                            for (Roles it : lstRoles) {
                                 if (rol.idRol.equals(it.idRol) )
                                    html += "<option value='" + it.idRol + "' selected>" + it.nombreRol + "</option>";
                                 else
                                    html += "<option value='" + it.idRol + "' >" + it.nombreRol + "</option>";
                            }

                            html += "</select>";
                        } else {
                            html += "Administrador Tecnico<input type='hidden' name='permisos' value=''>";

                        }
                        html += "</td></tr><tr><td class='st3' colspan='2' align='right'><input type='image' src='../images/10.png'></td></tr>"
                                + "</table>"
                                + "</form>";
                    }

                    if (opc.equals("passwd")) {
                        html += "<br><br><form method='post' name='formPaswd' target='_self' action='empleados.jsp' >"
                                + "<table width='' cellpadding='3' cellspacing='0'><input type='hidden' name='opc' value='guardar_passwd' />"
                                + "<input type='hidden' name='opt' value='empleados' />"
                                + "<input type='hidden' name='idEmpleado' value='"+idEmpleado+"' />"
                                + "<tr><td colspan='2' class='st1'>Cambiar Password</td></tr>"
                                + "<tr><td class='st3'>Clave :</td><td class='st4'><input type='password' id='clave' name='clave' value ='' size='50' /></td></tr>"
                                + "<tr><td class='st3'>Confirmar Clave :</td><td class='st4'><input type='password' id='confClave' name='confClave' value ='' size='50' /></td></tr>"
                                + "<tr><td class='st3' colspan='2' align='right'><input type='image' onclick='return formAdd_validar()' src='../images/10.png'></td></tr>"
                                + "</table>"
                                + "</form>";

                    }

                    if (opc.equals("alert")){
                        html += "<br><br><form method='post' name='formAlert' target='_self' action='empleados.jsp' >"
                                + "<table width='' cellpadding='3' cellspacing='0'>"
                                + "<input type='hidden' name='opt' value='empleados' />"
                                + "<tr><td colspan='2' class='st1'>"+alert+"</td></tr>"
                                + "<tr><td class='st3' colspan='2' align='right'><input type='image' onclick='return formAdd_validar()' src='../images/10.png'></td></tr>"
                                + "</table>"
                                + "</form>";
                    }
              
        

     

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Merchant Manager</title>
        <link href="../main.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../js/prototype.js"></script>
        <script type="text/javascript" src="../js/tablekit.js"></script>
        <script type="text/javascript" src="js/prototype.js"></script>
           <script type="text/javascript">
            function validarFormEmpleados() {
                if ($F('cedula')=="") {
                    $('cedula').focus();
                    alert('El campo es obligatorio.');
                    return false;
                }

                if ($F('nombre')=="") {
                    $('nombre').focus();
                    alert('El campo es obligatorio.');
                    return false;
                }
      
                if ($F('telefono')=="") {
                    $('telefono').focus();
                    alert('El campo es obligatorio.');
                    return false;
                }

                if ($F('correo')=="") {
                    $('correo').focus();
                    alert('El campo es obligatorio.');
                    return false;
                }
                return true;
            }
            </script>
    </head>
    <body>
<div id="page">
	<div id="header">
			<h1>Merchant Manager</h1>
			<div class="description">Usuario: <%=user.nombre%> / Empresa: <%=miEmpresa.nombre%> / Rol: <%=rolActual.nombreRol%> </div>
	</div>

	<div id="mainarea">
	<div id="sidebar">
		<div id="sidebarnav">
                    <a href="empresas.jsp">Empresas</a>
                    <a href="roles.jsp">Roles</a>
                    <a class="active" href="empleados.jsp">Empleados</a>
                    <a href="../cerrar.jsp">Cerrar Sesión</a>
                    <a href="../ayuda.jsp">Ayuda</a>
                </div>
        </div>

<div id="contentarea">


<%=html%>
               
</div>
</div>
<div id="footer">
Copyright &copy; 2011 - UNDeveloper
</div>
</div>
</body>
</html>
<%
}
%>